package com.blb.article_ms.controller;

import com.blb.article_ms.entity.ResponseResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class ValidationExceptionHandler {

    /**
     * 专门处理验证异常
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult<String> handleValidationException(MethodArgumentNotValidException ex){
        return ResponseResult.error(400L,ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }
}
